package com.asia.algorithmcode.greedy;

import java.util.Arrays;

public class Candy {

    public static void main(String[] args) {
        int[] arr = {1, 2, 2};
        System.out.println(new Candy().candy(arr));
    }

    public int candy(int[] ratings) {
        int n = ratings.length;
        int[] ans = new int[n];

        Arrays.fill(ans, 1);

        // 从左到右
        for (int i = 1; i < ratings.length; i++) {
            if (ratings[i] > ratings[i - 1]) {
                ans[i] = ans[i - 1] + 1;
            }
        }

        // 从右到左
        for (int i = n - 2; i >= 0; i--) {
            if (ratings[i] > ratings[i + 1]) {
                ans[i] = Math.max(ans[i], ans[i + 1] + 1);
            }
        }

        return Arrays.stream(ans).sum();
    }

}
